package com.cgsrp.cgsrp_user_province.service.impl;

import com.cgsrp.cgsrp_baby.domain.vo.PicVo;
import com.cgsrp.cgsrp_user_province.dao.ProvincesMapper;
import com.cgsrp.cgsrp_user_province.domain.ProvincesVo;
import com.cgsrp.cgsrp_user_province.service.ProvincesService;
import com.cgsrp.utils.Result;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.cgsrp.cgsrp_user_province.domain.Provinces;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ProvincesServiceImpl implements ProvincesService {

    @Resource
    private ProvincesMapper provincesMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public int insert(Provinces record) {
        return provincesMapper.insert(record);
    }

    @Override
    public int insertSelective(Provinces record) {
        return provincesMapper.insertSelective(record);
    }

    @Override
    public Result list() {
        HashOperations ops = redisTemplate.opsForHash();
        List<ProvincesVo> provinces = ops.values("PROVINCES");
        if (provinces.isEmpty()){
            provinces = provincesMapper.selectAll();
            for (ProvincesVo province : provinces) {
                ops.put("PROVINCES",province.getId(),province);
            }
            // 设置键的过期时间为1小时
            redisTemplate.expire("PROVINCES", 48, TimeUnit.HOURS);
        }
        return Result.suc("成功",provinces);
    }

    @Override
    public Provinces findById(Integer provinceId) {
        return provincesMapper.findById(provinceId);
    }
}
